package com.example.zbh.dsliao.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.ListView;

import com.example.zbh.dsliao.model.entity.LocalMusic;
import com.example.zbh.dsliao.ui.adapter.LocalMusicListViewAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 查找本地歌曲
 **/
public class FindSongs {
    //获取专辑封面的Uri 
    private static final Uri albumArtUri = Uri.parse("content://media/external/audio/albumart");

    public List<LocalMusic> getMp3Infos(ContentResolver contentResolver) {
        Cursor cursor = contentResolver.query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        List<LocalMusic> localMusics = new ArrayList<LocalMusic>();
        for (int i = 0; i < cursor.getCount(); i++) {
            LocalMusic localMusic = new LocalMusic();
            //新建一个歌曲对象,将从cursor里读出的信息存放进去,直到取完cursor里面的内容为止.
            cursor.moveToNext();


            long id = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media._ID));    //音乐id

            String title = cursor.getString((cursor
                    .getColumnIndex(MediaStore.Audio.Media.TITLE)));//音乐标题

            String artist = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ARTIST));//艺术家

            long duration = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DURATION));//时长

            long size = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media.SIZE));    //文件大小

            String url = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DATA));    //文件路径

            String album = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ALBUM)); //唱片图片

            long album_id = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); //唱片图片ID

            int isMusic = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐

            if (isMusic != 0 && duration / (1000 * 60) >= 1) {
                //只把1分钟以上的音乐添加到集合当中
                localMusic.setId(id);
                localMusic.setTitle(title);
                localMusic.setArtist(artist);
                localMusic.setDuration(duration);
                localMusic.setSize(size);
                localMusic.setUrl(url);
                localMusic.setAlbum(album);
                localMusic.setAlbum_id(album_id);
                localMusics.add(localMusic);
            }
        }
        return localMusics;
    }

    public void setListAdpter(Context context, List<LocalMusic> mp3Infos, ListView mMusicList) {
        List<HashMap<String, String>> mp3list = new ArrayList<HashMap<String, String>>();
        LocalMusicListViewAdapter adapter = new LocalMusicListViewAdapter(context, mp3Infos);
        mMusicList.setAdapter(adapter);
    }
}

